package com.fary.common;

import java.io.Serializable;

//@ApiModel(value = "接口统一返回数据")
public class ResponseVo<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    public static final int SUCCESS = 0;

    public static final int FAIL = -1;
    public static final int BUSY = -100;

    public static final String SUCCESS_TEXT = "Success";
    public static final String FAIL_TEXT = "Fail";
    public static final String BUSY_TEXT = "Busy";

//    @ApiModelProperty(name = "status", value = "响应码")
    private long status;
//    @ApiModelProperty(name = "statustext", value = "消息")
    private String statustext;
//    @ApiModelProperty(name = "data", value = "数据")
    private T data; // NOSONAR

    public ResponseVo() {
        status = SUCCESS;
        statustext = SUCCESS_TEXT;
    }

    public ResponseVo(long status, String statustext, T data) {
        super();
        this.status = status;
        this.statustext = statustext;
        this.data = data;
    }

    public ResponseVo(T data) {
        super();
        if (data instanceof Exception) {
            status = -1;
            statustext = ((Exception) data).getLocalizedMessage();
        } else {
            status = SUCCESS;
            statustext = SUCCESS_TEXT;
            this.data = data;
        }
    }

    public long getStatus() {
        return status;
    }

    public void setStatus(long status) {
        this.status = status;
        if (status == SUCCESS) {
            this.statustext = SUCCESS_TEXT;
        } else if (status == BUSY) {
            this.statustext = BUSY_TEXT;
        } else {
            this.statustext = "";
        }
    }

    public String getStatustext() {
        return statustext;
    }

    public void setStatustext(String statustext) {
        this.statustext = statustext;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    /**
     * 返回执行成功的响应
     *
     * @param <M> 类型
     * @return 执行成功的响应
     */
    public static <M> ResponseVo<M> ok() {
        return new ResponseVo(SUCCESS, SUCCESS_TEXT, "");
    }

    /**
     * 返回执行成功的响应
     *
     * @param <T>  类型
     * @param data 返回数据
     * @return 执行成功的响应
     */
    public static <T> ResponseVo<T> ok(T data) {
        return new ResponseVo<>(data);
    }


    public static <M> ResponseVo<M> fail(String errorMessage) {
        return new ResponseVo(-1, errorMessage, "");
    }

}
